package com.ykyl.controller.security;


import com.ykyl.base.PageResponse;
import com.ykyl.base.Result;
import com.ykyl.pojo.dto.RoleDto;
import com.ykyl.pojo.vo.RoleVo;
import com.ykyl.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色前端控制器
 */
@Slf4j
@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    RoleService roleService;

    /***
     *  多条件查询角色分页列表
     * @param roleDto 角色DTO查询条件
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return: Page<RoleVo>
     */
    @PostMapping("page/{pageNum}/{pageSize}")
    public Result<PageResponse<RoleVo>> findRoleVoPage(
                                    @RequestBody RoleDto roleDto,
                                    @PathVariable("pageNum") int pageNum,
                                    @PathVariable("pageSize") int pageSize) {
        PageResponse<RoleVo> roleVoPage = roleService.findRolePage(roleDto, pageNum, pageSize);
        return Result.success(roleVoPage);
    }

    /**
     *  保存角色
     * @param roleDto 角色DTO对象
     * @return RoleVo
     */
    @PutMapping
    public Result<RoleVo> createRole(@RequestBody RoleDto roleDto) {
        RoleVo roleVoResult = roleService.createRole(roleDto);
        return Result.success(roleVoResult);
    }

    /**
     *  修改角色
     * @param roleDto 角色Vo对象
     * @return Boolean 是否修改成功
     */
    @PatchMapping
    public Result<Boolean> updateRole(@RequestBody RoleDto roleDto) {
        Boolean flag = roleService.updateRole(roleDto);
        return Result.success(flag);
    }

    /**
     *  角色下拉框
     * @return
     */
    @PostMapping("init-roles")
    Result<List<RoleVo>> initRoles() {
        List<RoleVo> roleVoResult = roleService.initRoles();
        return Result.success(roleVoResult);
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/{roleIds}")
    public Result remove(@PathVariable List<Long> roleIds) {
        return Result.success(roleService.deleteRoleByIds(roleIds));
    }
}
